const Router = require('koa-router')

const { auth } = require('../middleware/auth.middleware')
const { validator } = require('../middleware/cart.middleware')
const { validateQuery } = require('../middleware/goods.middleware')

const {
  add,
  findAll,
  update,
  remove,
  selectAll,
  unselectAll,
  countCart,
} = require('../controller/cart.controller')
const { findOne } = require('../controller/good.controller')

const router = new Router({ prefix: '/carts' })

// 添加商品到购物车
router.post('/', auth, validator({ goods_id: 'number' }), findOne, add)

// 购物车列表
router.get('/', auth, validateQuery, findAll)

// 更新购物车
router.patch(
  '/:id',
  auth,
  validator({
    number: { type: 'number', required: false, min: 0 },
    selected: { type: 'boolean', required: false },
  }),
  update
)

// 批量删除购物车数据
router.delete('/', auth, validator({ ids: 'array' }), remove)

// 全选
router.post('/selectAll', auth, selectAll)
// 全不选
router.post('/unselectAll', auth, unselectAll)

// 当前登录用户购物车内商品总数 可显示在tab栏 badge
router.get('/total', auth, countCart)

module.exports = router
